
var specs_obj = {};		// 记录已选择的规格的价格
var quantity_obj = {};	// 记录已选择的规格的库存数量

var evaluation_pages = {
	'all'	: 1
	,'1'	: 0
	,'2'	: 0
	,'3'	: 0
	,'4'	: 0
	,'5'	: 0
}
evaluation_type		= 'all';
var view_history	= [goodsid];

jQuery(function($){
	/*******************************************************************************
	 * jQzoom放大
	 *******************************************************************************/
	$('a.jqzoom').jqzoom({
		zoomType		: 'reverse'
		,lens			: true
		,preloadImages	: true
		,alwaysOn		: false
		,zoomWidth		: 788
		,zoomHeight		: 500
		,xOffset		: 9

		,preloadText	: '大图加载中 ...'
	});
	
	// 收藏，用Ajax
	$('input.favorite').click(function(){
		if(__ajaxing){
			$(document).queue("ajax", (function(goodsid){
				return function(){add_to_favorite(goodsid);}
			})(goodsid));
		}else{
			add_to_favorite(goodsid);
		}
	});

	/*******************************************************************************
	 * 规格选择处理
	 *******************************************************************************/
	$('#specs_all a[dir]').click(function(){
		$(this).addClass('cur').parents('dd').siblings('dd').find('a').removeClass('cur');

		var dir = ($(this).attr('dir')).split('-');
		$('input[name="specs['+dir[0]+']"]').val(dir[1]);	// 已选的规格及值保存到隐藏表单
		// 计算当前最大价格
		var price = specs_json[dir[0]]['value'][dir[1]]['price'];
		specs_obj[dir[0]] = price = parseFloat(price);
		if(price > 0){	
			for(var i in specs_obj){
				if(specs_obj[i] > price){
					price = specs_obj[i];
				}
			}
			$('#goods_price').text('￥'+price);
		}
		
		// 库存数量
		var autofill = $.trim(specs_json[dir[0]]['value'][dir[1]]['autofill']);
		if(autofill == '1'){	// 采用商品默认库存
			quantity = goods_quantity;
		}else{
			quantity = specs_json[dir[0]]['value'][dir[1]]['quantity'];
		}
		quantity_obj[dir[0]] = quantity = parseInt(quantity);
		for(var i in quantity_obj){
			if(quantity_obj[i] < quantity){
				quantity = quantity_obj[i];
			}
		}
		$('#goods_stock').text(quantity);

		return false;
	});
	
	/*******************************************************************************
	 * 数量
	 *******************************************************************************/
	$('#number_sub').click(function(){
		var number = parseInt($('#number').val());
		if(number == NaN || number <= 1){
			number = 1;
		}else{
			number --;
		}
		$('#number').val(number);
		return false;
	});
	$('#number_add').click(function(){
		var number = parseInt($('#number').val());
		if(number == NaN || number < 1){
			number = 1;
		}else{
			number ++;
		}
		if(number > quantity){
			showmsg_warning('库存不够，库存：'+quantity+quantityunit);
			return false;
		}
		$('#number').val(number);
		return false;
	});

	/*******************************************************************************
	 * 购物提交前判断
	 *******************************************************************************/
	$('form#fm_goods').submit(function(evt){
		var errmsg = [];
		var exist = null;
		var value = null;
		var regexp = null;
		var fm = $('form#fm_goods');
		//var number = $.trim(fm.find('input[name="number"]').val());
		var number = $.trim($('#number').val());
		if(number == ''){
			errmsg.push('未输入数量');
		}
		number = parseInt(number);
		if(number <= 0){
			errmsg.push('请输入合法的数量');	// 下一步需要判断具体规格的数量
		}
		for(var i in specs_json){
			if(specs_json[i]['type'] == 'string'){
				value = $.trim($('input[name="specs['+i+']"]').val());
				if(value == ''){
					if(specs_info[i]['empty'] == 0){
						errmsg.push('未输入：'+specs_json[i]['name']);
					}
				}else{
					regexp = '^.+$';
					if(specs_info[i]['regexp'] != ''){
						var regexp = specs_info[i]['regexp'];
					}
					regexp = new RegExp(regexp);
					if(regexp.test(value) === false){
						if(specs_info[i]['prompt'] != ''){
							errmsg.push(specs_info[i]['prompt']);
						}else{
							errmsg.push('格式错：'+specs_json[i]['name']);
						}
					}
				}
			}else{
				if(specs_info[i]['empty'] == 1){
					continue;
				}
				exist = false;
				for(var j in specs_obj){
					if(i == j){
						exist = true;
						break;
					}
				}
				if(!exist){
					if(specs_info[i]['prompt'] != ''){
						errmsg.push(specs_info[i]['prompt']);
					}else{
						errmsg.push('未选择：'+specs_json[i]['name']);
					}
				}
			}
		}
		if(errmsg.length > 0){
			evt.iscancelled = true;
		errmsg = errmsg.join('<br />');
			showmsg_warning(errmsg);
			return false;
		}
		if(quantity <= 0 || number > quantity){
			evt.iscancelled = true;	// 阻止提交事件

			showmsg_warning('库存不够，库存：'+quantity+quantityunit);
			return false;
		}
		return true;					
	});

	/*******************************************************************************
	 * 评价
	 *******************************************************************************/

	$('#evaluation_tab > a').hover(function(){
		var type = $.trim((($(this).attr('href')).split('#')).pop());
		evaluation_type = type;
		//console.log(type);
		if(evaluation_pages[type] == 0){
			evaluation_pages[type] = 1;
			if(__ajaxing){
				$(document).queue("ajax", (function(type){
					return function(){get_comments(1, type);}
				})(type));
			}else{
				get_comments(1, evaluation_type);
			}
		}
	}, function(){});

	// 评价翻页
	/*$('div.pagelist a').live('click', function(){
		var page = $(this).attr('rel');
		if(page > 0){
			get_comments(page, evaluation_type);
		}
		return false;
	});*/
	/*$('b.go').live('click', function(){
		var fm = $(this).parents('form');
		var pagecount = $.trim(fm.find('input[name="pagecount"]').val());
		var page = parseInt(fm.find('input[name="page"]').val());
		if(page > 0 && page <= pagecount){
			get_comments(page, evaluation_type);
		}
	});*/

	// 加载评价
	$(document).queue("ajax", function(){
		get_comments(1, 'all');
	});

	/*******************************************************************************
	 * 浏览历史
	 *******************************************************************************/
	var _str = $.trim(get_cookie('view_history'));
	if(_str != ''){
		var arr = _str.split(',');

		for(var i = 0; i < arr.length && i < 9; i ++){
			if(arr[i] == goodsid){
				continue;
			}
			view_history.push(arr[i]);
		}
	}
	if(view_history.length > 0){
		set_cookie('view_history', view_history.join(','));
		view_history.shift();
		$(document).queue("ajax", function(){
			get_view_history_goods(view_history);
		});
	}
});

// 收藏
function add_to_favorite(goodsid){
	$.post(
		_s.web_dir+'index.php?frm=ajax&mdl=goods&do=favorite'
		,{'goodsid':goodsid}
		,function(data){
			if (data == 'succ') {
				showmsg_right('收藏成功');
			}else{
				if(data == 'exist'){
					showmsg_warning('您已经收藏过该商品了', {timeout:5000});
				}else{
					showmsg_error('收藏失败');
				}
			}
		}
	);
}

/*******************************************************************************
 * 这里实现直接页码跳转
 *******************************************************************************/
function html_go(page){
	get_comments(page, evaluation_type);
}

function get_comments(page, type){
	//console.log(type);
	evaluation_pages[type] = page;
	$.get(
		_s.web_dir+'index.php?frm=ajax&mdl=goods&do=comment',
		{'goodsid' : goodsid, 'type' : type, 'page' : page},
		function(data){
			$('div#evaluation_'+type).html(data);
		}
	);
}

function get_view_history_goods(goods_ids){
	$.get(
		_s.web_dir+'index.php?frm=ajax&mdl=goods&do=history',
		{'goods_ids' : goods_ids},
		function(data){
			$('#view_history').html(data).fadeIn(800);

		}
	);
}
